<!-- 注册登录界面 -->
<template>
    <div class="bg">
      <div class="box">
        <div class="pre-box" ref="preRef">
          <h1>Welcome to 社区</h1>
          <p>JOIN US!</p>
          <div class="img-box">
            <img src="@/assets/images/logo.png" alt="" />
          </div>
        </div>
  
        <div class="register-form">
          <el-form
            :rules="registerRules"
            :model="form"
            ref="registerForm"
            label-width="0px"
          >
            <div class="title-box">
              <h1>注册</h1>
            </div>
            <div class="input-box">
              <el-form-item prop="username">
                <el-input v-model="form.username" placeholder="用户名"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input show-password v-model="form.password" placeholder="密码"></el-input>
              </el-form-item>
              <el-form-item prop="password2">
                <el-input
                  show-password
                  v-model="form.password2"
                  placeholder="确认密码"
                ></el-input>
              </el-form-item>
            </div>
            <div class="btn-box">
              <button @click="regist">注册</button>
              <p @click="isLogin = true">已有账号？去登录</p>
            </div>
          </el-form>
        </div>
  
        <div class="login-form">
          <el-form
            :model="form"
            ref="loginForm"
            label-width="0px"
            :inline="false"
            size="normal"
          >
            <div class="title-box">
              <h1>登录</h1>
            </div>
            <div class="input-box">
              <el-form-item prop="username">
                <el-input v-model="form.username" placeholder="用户名"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input show-password v-model="form.password" placeholder="密码"></el-input>
              </el-form-item>
            </div>
            <div class="btn-box">
              <el-form-item>
                <button @click.prevent="gotoHome">登录</button>
                <p style="display: inline-block" @click="isLogin = false">
                  没有账号？去注册
                </p>
              </el-form-item>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  import { login,register } from "@/api/login.js";
  import { setToken } from "@/utils/auth";
  export default {
    name: "login",
    data() {
      var validatePass2 = (rule, value, call) => {
        if (value === "") {
          call(new Error("确认密码不能为空！"));
          // password 是表单上绑定的字段
        } else if (value !== this.form.password) {
          call(new Error("两次输入密码不一致!"));
        } else {
          call();
        }
      };
      return {
        isLogin: true,
        isShow: true,
        flag: false,
        form: {
          username: "",
          password: "",
          password2: "",
        },
        registerRules: {
          username: [
            { required: true, message: "用户名不能为空！", trigger: "blur" },
          ],
          password: [
            { required: true, message: "密码不能为空！", trigger: "blur" },
          ],
          password2: [
            {
              required: true,
              validator: validatePass2,
              trigger: "blur",
            },
          ],
        },
      };
    },
    methods: {
      gotoHome: function () {
        console.log("登录");
        //用户登录
        login(this.form).then((response) => {
          // 登录成功记录token和用户信息，登录失败给对应提示
          console.log(response)
          setToken(response.data.token);
          // 存储用户信息
          localStorage.setItem("userInfo", JSON.stringify(response.data.userInfo));
          const lastUrl = localStorage.getItem("logUrl");
          if (lastUrl && "/login" != lastUrl) {
            this.$router.push({ path: localStorage.getItem("logUrl") });
          } else {
            this.$router.push({ path: "/" });
          }
        });
        // this.$store.dispatch('user/login',this.form).then(()=>{
        //   const lastUrl = localStorage.getItem("logUrl");
        //   if (lastUrl && "/login" != lastUrl) {
        //     this.$router.push({ path: localStorage.getItem("logUrl") });
        //   } else {
        //     this.$router.push({ path: "/" });
        //   }
        // })
      },
      regist() {
        this.$refs.registerForm.validate((valid) => {
          if (valid) {
            register({
              username: this.form.username,
              password: this.form.password,
            }).then((res) => {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.isLogin = true;
              this.form.password2 = "";
            });
          }
        });
      },
    },
    watch: {
      isLogin: {
        handler() {
          this.$refs.preRef.value = this.isLogin;
          console.log("欢迎来到社区！", "isLgoin：", this.isLogin);
  
          if (!this.isLogin) {
            this.$refs.preRef.style.transform = "translateX(100%)";
          } else {
            this.$refs.preRef.style.transform = "translateX(0)";
          }
        },
      },
    },
  };
  </script>
  
  
  
  <style scoped>
  /* 2023 5.11 18:58
      未使用transiton成功使用动画效果，需把v-show这种方法也写出来
   
  */
  
  /* v-show动画
  .shan-enter-active{
      animation: cartoon 2s forwards linear;
    
     
  }
  
  @keyframes cartoon {
      from{
          transform: translateX(0);
  
      }
      to{
          transform: translateX(100%);
      }
      
  } */
  * {
    margin: 0px;
    padding: 0px;
  }
  .bg {
    background-color: rgb(173, 222, 243);
    height: 100vh;
    display: flex;
  }
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  input {
    outline: none;
  }
  .box {
    width: 750px;
    height: 375px;
    display: flex;
    position: relative;
    /* background-color: #fff; */
    margin: auto;
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.6);
    box-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
  }
  .pre-box {
    width: calc(750px / 2);
    height: 100%;
    left: 0;
    top: 0;
    position: absolute;
    border-radius: 4px;
    background-color: skyblue;
    box-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
    transition: 0.75s ease-in-out;
    z-index: 1;
  }
  .pre-box h1 {
    margin-top: 40px;
    text-align: center;
    letter-spacing: 5px;
    color: white;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
  }
  .pre-box p {
    height: 30px;
    line-height: 20px;
    text-align: center;
    margin: 20px 0;
    font-weight: bold;
    color: white;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
    animation-fill-mode: forwards;
  }
  
  .img-box {
    width: 150px;
    height: 150px;
    margin: 20px auto;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 7px 8px 6px rgba(0, 0, 0, 0.3);
    transition: 0.5s;
  }
  .img-box img {
    width: 100%;
  }
  
  .login-form,
  .register-form {
    flex: 1;
    height: 100%;
  }
  .title-box {
    height: 120px;
    line-height: 150px;
  }
  .title-box h1 {
    text-align: center;
    letter-spacing: 5px;
    color: white;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
  }
  .input-box {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  input {
    width: 60%;
    height: 35px;
    margin-bottom: 15px;
    text-indent: 5px;
    border: 1px solid #b0cfe9;
    border-radius: 15px;
  }
  input:focus::placeholder {
    opacity: 0;
  }
  .btn-box {
    display: flex;
    justify-content: center;
  }
  button {
    width: 100px;
    height: 30px;
    margin-right: 7px;
    line-height: 30px;
    border: none;
    border-radius: 15px;
    background-color: rgb(72, 191, 212);
    color: white;
  }
  button:hover {
    cursor: pointer;
    opacity: 0.8;
  }
  .btn-box p {
    height: 30px;
    line-height: 30px;
    font-size: 14px;
    color: white;
  }
  .btn-box p:hover {
    cursor: pointer;
    border-bottom: 1px solid white;
  }
  .el-input {
    width: 230px;
  }
  div /deep/.el-input__inner {
    border-radius: 15px;
  }
  /* div /deep/ .el-input__inner:focus {
  } */
  div /deep/ .el-form-item {
    margin-bottom: 25px;
  }
  </style>